/**
 * 加载商品列表数据
 */
function load() {
    let cartListStr = localStorage.getItem('CARTLIST')
    let cartList = JSON.parse(cartListStr) || []

    $.ajax({
        type: 'get',
        url: 'http://www.xiongmaoyouxuan.com/api/detail',
        data: {
            id: cartList[0],
            normal: 1,
            sa: ''
        },
        success: function (result) {
            // console.log(result);
            if (result.code == 200) {
                let shop = result.data
                // console.log(shop);
                Panda(shop)
            }
        }
    })
}
function Panda(i) {
    let cart = i.shop
    let photo = i.detail.photo
    let dCtList = i.detail.descContentList
    let cst = i.detail
    let src = dCtList.map((item, index) => {
        return dCtList[index].photo
    })
    // console.log(src);
    // 买家信息渲染
    let data = {
        cart
    }
    let htmlStr = template('cp', data)
    $('.cmt-b').html(htmlStr)
    // 轮播图渲染
    let con = {
        photo
    }
    // console.log(con);
    let htmlStr1 = template('lunBo', con)
    // console.log(htmlStr1);
    $('.pts').html(htmlStr1)

    let dct = {
        src
    }
    // console.log(dct);
    let htmlStr2 = template('imeGs', dct)
    // console.log(htmlStr2);
    $('.imgs').html(htmlStr2)
    let sju = {
        cst
    }
    // console.log(sju);
    let htmlStr3 = template('can-t', sju)
    // console.log(htmlStr3);
    $('.can').html(htmlStr3)

}


// 加入购物车

function onShopping(id) {
    $.ajax({
        type: 'get',
        url: 'http://www.xiongmaoyouxuan.com/api/detail?',
        data: {
            id,
            normal: 1,
            sa: ''
        },
        success: function (result) {
            // console.log(result);
            if (result.code == 200) {
                let shop = result.data
                // console.log(shop);
                let sum = {
                    id: shop.shop.id,
                    num: 1,
                    img: shop.detail.image,
                    price: shop.detail.price,
                    name: shop.detail.qunTitle,
                    state: false,
                    singlePrice: 0,
                }
                onAddCart(sum)
            }
        }
    })
}

function onAddCart(i) {
    console.log(i);
    let cartListStr = localStorage.getItem('CARTS')
    let cartList = JSON.parse(cartListStr) || []
    // cartList.push({
    //     id
    //     num:1
    // })

    // console.log(i.shop.id);
    // let newProduct = cartList.find(item => item.shop.id==i.shop.id)
    // if(newProduct){
    //     let sum = {
    //         num:1,
    //         img:i.detail.image,
    //         price:i.detail.price,
    //         qunTitle:i.detail.qunTitle
    //     }
    // }else{
    // //3.2 如果没有直接添加
    // cartList.unshift(i)
    // }

    let duct = cartList.find(item => item.id == i.id)
    if (duct) {
        duct.num++
    } else {
        // //3.2 如果没有直接添加
        cartList.push({
            id: i.id,
            num: 1,
            img: i.img,
            price: i.price,
            name: i.name,
            state: false,
            singlePrice: i.price,
        })

    }
    // // 4. 添加了新的菜品的数组重新写入localstorage
    localStorage.setItem('CARTS', JSON.stringify(cartList))

    // 5. 是否显示购物
    if (confirm('添加成功,是否进入购物车?')) {
        location.href = './Shopping.html'
    }
}
load()

function homepage() {
    location.href = './index.html'
}



